package com.getpebble.android.framework.notification.gmail;

import java.util.Map;
import java.util.logging.Logger;
import myjavax.security.auth.callback.CallbackHandler;
import myjavax.security.sasl.SaslClient;
import myjavax.security.sasl.SaslClientFactory;

public class OAuth2SaslClientFactory implements SaslClientFactory {
    public static final String OAUTH_TOKEN_PROP = "mail.imaps.sasl.mechanisms.oauth2.oauthToken";
    private static final Logger logger = Logger.getLogger(OAuth2SaslClientFactory.class.getName());

    public SaslClient createSaslClient(String[] mechanisms, String authorizationId, String protocol, String serverName, Map<String, ?> props, CallbackHandler callbackHandler) {
        boolean matchedMechanism = false;
        for (String equalsIgnoreCase : mechanisms) {
            if ("XOAUTH2".equalsIgnoreCase(equalsIgnoreCase)) {
                matchedMechanism = true;
                break;
            }
        }
        if (matchedMechanism) {
            return new OAuth2SaslClient((String) props.get(OAUTH_TOKEN_PROP), callbackHandler);
        }
        logger.info("Failed to match any mechanisms");
        return null;
    }

    public String[] getMechanismNames(Map<String, ?> map) {
        return new String[]{"XOAUTH2"};
    }
}
